﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Lumia.Imaging;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Graphics.Display;
using Lumia.Imaging.Adjustments;
using System.Reflection;
using Windows.ApplicationModel.DataTransfer;
using System.Diagnostics;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace pintu
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Lumiaimagepage : Page
    {
        private GrayscaleEffect _grayscaleEffect;
        private BrightnessEffect _brightnessEffect;
        private SwapChainPanelRenderer m_renderer;

        // The following  WriteableBitmap contains
        // the filtered and thumbnail image.
        private WriteableBitmap _writeableBitmap;
        private WriteableBitmap _thumbnailImageBitmap;
        List<string> effectsnamelist = new List<string>();
        private IImageProvider currenteffect = null;
        public Lumiaimagepage()
        {
            this.InitializeComponent();
            double scaleFactor = 1.0;
            scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;

            _writeableBitmap = new WriteableBitmap((int)(Window.Current.Bounds.Width * scaleFactor), (int)(Window.Current.Bounds.Height * scaleFactor));
            _thumbnailImageBitmap = new WriteableBitmap((int)OriginalImage.Width, (int)OriginalImage.Height);
            _grayscaleEffect = new GrayscaleEffect();
            _brightnessEffect = new BrightnessEffect(_grayscaleEffect);
            m_renderer = new SwapChainPanelRenderer(_brightnessEffect, SwapChainPanelTarget);
            effectsnamelist.Add("AutoEnhanceEffect");
            effectsnamelist.Add("AutoLevelsEffect");
            effectsnamelist.Add("BlurEffect");
            effectsnamelist.Add("ColorAdjustEffect");
            effectsnamelist.Add("ColorBoostEffect");
            effectsnamelist.Add("ColorizationEffect");
            effectsnamelist.Add("ContrastEffect");
            effectsnamelist.Add("CurvesEffect");
            effectsnamelist.Add("DespeckleEffect");
            effectsnamelist.Add("ExposureEffect");
            effectsnamelist.Add("GaussianNoiseEffect");
            effectsnamelist.Add("GrayscaleEffect");
            effectsnamelist.Add("HdrEffect");
            effectsnamelist.Add("HueSaturationEffect");
            effectsnamelist.Add("HueSaturationLightnessEffect");
            effectsnamelist.Add("LensBlurEffect");
            effectsnamelist.Add("LevelsEffect");
            effectsnamelist.Add("LocalBoostAutomaticEffect");
            effectsnamelist.Add("LocalBoostEffect");
            effectsnamelist.Add("NoiseEffect");
            effectsnamelist.Add("RgbLevelsEffect");
            effectsnamelist.Add("RgbMixerEffect");
            effectsnamelist.Add("SaturationLightnessEffect");
            effectsnamelist.Add("SharpnessEffect");
            effectsnamelist.Add("TemperatureAndTintEffect");
            effectsnamelist.Add("VibranceEffect");
            effectsnamelist.Add("WhiteBalanceEffect");
            effectsnamelist.Add("WhiteboardEnhancementEffect");
            effectlistbox.ItemsSource = effectsnamelist;
            currenteffect = _grayscaleEffect;
            // LoadDefaultImageAsync();

        }
        private async void LoadDefaultImageAsync()
        {
            //var file = await StorageFile.GetFileFromApplicationUriAsync(new System.Uri("ms-appx:///Assets/defaultImage.jpg"));
            await ApplyEffectAsync(tranferedfile);
        }
        StorageFile tranferedfile = null;
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            tranferedfile = e.Parameter as StorageFile;
            LoadDefaultImageAsync();
            base.OnNavigatedTo(e);
        }
        private async Task<bool> ApplyEffectAsync(StorageFile file)
        {
            // Open a stream for the selected file.
            IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);

            string errorMessage = null;

            try
            {
                // Show a thumbnail of the original image.
                _thumbnailImageBitmap.SetSource(fileStream);
                OriginalImage.Source = _thumbnailImageBitmap;

                // Rewind the stream to start.
                fileStream.Seek(0);

                // Set the imageSource on the effect and render.
                ((IImageConsumer)_grayscaleEffect).Source = new Lumia.Imaging.RandomAccessStreamImageSource(fileStream);
                await m_renderer.RenderAsync();

            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                var dialog = new MessageDialog(errorMessage);
                await dialog.ShowAsync();
                return false;
            }

            return true;
        }
        private async Task<bool> ApplyEffectAsync(StorageFile file, IImageProvider effect)
        {
            // Open a stream for the selected file.
            IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
            currenteffect = effect;
            string errorMessage = null;

            try
            {
                // Show a thumbnail of the original image.
                _thumbnailImageBitmap.SetSource(fileStream);
                OriginalImage.Source = _thumbnailImageBitmap;

                // Rewind the stream to start.
                fileStream.Seek(0);

                // Set the imageSource on the effect and render.
                ((IImageConsumer)effect).Source = new Lumia.Imaging.RandomAccessStreamImageSource(fileStream);
                await m_renderer.RenderAsync();

            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                //var dialog = new MessageDialog(errorMessage);
                //await dialog.ShowAsync();
                return false;
            }

            return true;
        }
        private async void OnBrightnessChanged(object sender, RangeBaseValueChangedEventArgs e)
        {
            Debug.WriteLine(slider.Value);
                
            if (str == "AutoEnhanceEffect")
            {
                AutoEnhanceEffect autoeffect = new AutoEnhanceEffect() { WhiteBalanceMode = WhiteBalanceMode.Manual };
                m_renderer = new SwapChainPanelRenderer(autoeffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, autoeffect);
            }
            if (str == "AutoLevelsEffect")
            {
                AutoLevelsEffect blueffect = new AutoLevelsEffect() { };              
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "BlurEffect")
            {
                BlurEffect blueffect = new BlurEffect() { };
                blueffect.KernelSize = (int)slider.Value;
                 m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ColorAdjustEffect")
            {
                ColorAdjustEffect blueffect = new ColorAdjustEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ColorBoostEffect")
            {
                ColorBoostEffect blueffect = new ColorBoostEffect() { };             
                blueffect.Gain = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ColorizationEffect")
            {
                ColorizationEffect blueffect = new ColorizationEffect() { };              
                blueffect.Luminance = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ContrastEffect")
            {
                ContrastEffect blueffect = new ContrastEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);               
                blueffect.Level = slider.Value;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "CurvesEffect")
            {
                CurvesEffect blueffect = new CurvesEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "DespeckleEffect")
            {
                DespeckleEffect blueffect = new DespeckleEffect() { };
                blueffect.DespeckleLevel = DespeckleLevel.High;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ExposureEffect")
            {
                ExposureEffect blueffect = new ExposureEffect() { };               
                blueffect.Gain = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "GaussianNoiseEffect")
            {
                GaussianNoiseEffect blueffect = new GaussianNoiseEffect() { };             
                blueffect.Level = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "GrayscaleEffect")
            {
                GrayscaleEffect blueffect = new GrayscaleEffect() { };               
                blueffect.Constant = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "HdrEffect")
            {
                HdrEffect blueffect = new HdrEffect() { };               
                blueffect.Gamma = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "HueSaturationEffect")
            {
                HueSaturationEffect blueffect = new HueSaturationEffect() { };               
                blueffect.Saturation = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "HueSaturationLightnessEffect")
            {
                HueSaturationLightnessEffect blueffect = new HueSaturationLightnessEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LensBlurEffect")
            {
                LensBlurEffect blueffect = new LensBlurEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LevelsEffect")
            {
                LevelsEffect blueffect = new LevelsEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LocalBoostAutomaticEffect")
            {
                LocalBoostAutomaticEffect blueffect = new LocalBoostAutomaticEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LocalBoostEffect")
            {
                LocalBoostEffect blueffect = new LocalBoostEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "NoiseEffect")
            {
                NoiseEffect blueffect = new NoiseEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "RgbLevelsEffect")
            {
                RgbLevelsEffect blueffect = new RgbLevelsEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "RgbMixerEffect")
            {
                RgbMixerEffect blueffect = new RgbMixerEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "SaturationLightnessEffect")
            {
                SaturationLightnessEffect blueffect = new SaturationLightnessEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "SharpnessEffect")
            {
                SharpnessEffect blueffect = new SharpnessEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "TemperatureAndTintEffect")
            {
                TemperatureAndTintEffect blueffect = new TemperatureAndTintEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "VibranceEffect")
            {
                VibranceEffect blueffect = new VibranceEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "WhiteBalanceEffect")
            {
                WhiteBalanceEffect blueffect = new WhiteBalanceEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "WhiteboardEnhancementEffect")
            {
                WhiteboardEnhancementEffect blueffect = new WhiteboardEnhancementEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
        
           
        }
        private void PickImage_Click(object sender, RoutedEventArgs e)
        {
          
           // SaveButton.IsEnabled = false;
            var openPicker = new FileOpenPicker
            {
                SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                ViewMode = PickerViewMode.Thumbnail
            };

            // Filter to include a sample subset of file types.
            openPicker.FileTypeFilter.Clear();
            openPicker.FileTypeFilter.Add(".bmp");
            openPicker.FileTypeFilter.Add(".png");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".jpg");

            PickImage(openPicker);
        }
        private async void PickImage(FileOpenPicker openPicker)
        {
            // Open the file picker.
            StorageFile file = await openPicker.PickSingleFileAsync();

            // File is null if the user cancels the file picker.
            if (file != null)
            {
                if (!(await ApplyEffectAsync(file)))
                    return;

                SaveButton.IsEnabled = true;
            }
        }

        private async void SaveImage(FileSavePicker savePicker)
        {
            var file = await savePicker.PickSaveFileAsync();
            if (file != null)
            {
                await SaveImageAsync(file);
            }

            SaveButton.IsEnabled = true;
        }
        private async Task<bool> SaveImageAsync(StorageFile file)
        {
            if (_grayscaleEffect == null)
            {
                return false;
            }

            string errorMessage = null;

            try
            {
                using (var jpegRenderer = new JpegRenderer(_grayscaleEffect))
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    // Jpeg renderer gives the raw buffer that contains the filtered image.
                    IBuffer jpegBuffer = await jpegRenderer.RenderAsync();
                    await stream.WriteAsync(jpegBuffer);
                    await stream.FlushAsync();
                }
            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                var dialog = new MessageDialog(errorMessage);
                await dialog.ShowAsync();
                return false;
            }
            return true;
        }
        private async Task<bool> SaveImageAsync(StorageFile file,IImageProvider provider)
        {
            if (provider == null)
            {
                return false;
            }

            string errorMessage = null;

            try
            {
                using (var jpegRenderer = new JpegRenderer(provider))
                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    // Jpeg renderer gives the raw buffer that contains the filtered image.
                    IBuffer jpegBuffer = await jpegRenderer.RenderAsync();
                    await stream.WriteAsync(jpegBuffer);
                    await stream.FlushAsync();
                }
            }
            catch (Exception exception)
            {
                errorMessage = exception.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                var dialog = new MessageDialog(errorMessage);
                await dialog.ShowAsync();
                return false;
            }
            return true;
        }
        private void SaveImage_Click(object sender, RoutedEventArgs e)
        {

        }
        string str;
        private async void TextBlock_Tapped(object sender, TappedRoutedEventArgs e)
        {
            double miniumvalue = 0;
            double maxvalue = 0;
            //string str = "Lumia.Imaging.Adjustments"+(sender as TextBlock).Text;
            //Assembly assembly = Assembly.Load("程序集路径，不能是相对路径"); // 加载程序集（EXE 或 DLL） 
            //Type obj = assembly.GetType(str);// 创建类的实例 
            //object objdd = Activator.CreateInstance(obj);
            str = (sender as TextBlock).Text;
            if(str== "AutoEnhanceEffect")
            {
                slider.Visibility = Visibility.Collapsed;
                AutoEnhanceEffect autoeffect = new AutoEnhanceEffect() {  WhiteBalanceMode= WhiteBalanceMode .Manual};
                m_renderer = new SwapChainPanelRenderer(autoeffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, autoeffect);
            }
            if (str == "AutoLevelsEffect")
            {
                AutoLevelsEffect blueffect = new AutoLevelsEffect() {  };
                slider.Visibility = Visibility.Collapsed;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "BlurEffect")
            {
                BlurEffect blueffect = new BlurEffect() { };
                miniumvalue = 1;
                maxvalue = 256;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ColorAdjustEffect")
            {
                ColorAdjustEffect blueffect = new ColorAdjustEffect() {  };
                slider.Visibility = Visibility.Collapsed;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ColorBoostEffect")
            {
                ColorBoostEffect blueffect = new ColorBoostEffect() {  };
                miniumvalue = -1.0;
                maxvalue = 20.0;
                //blueffect.Gain = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ColorizationEffect")
            {
                ColorizationEffect blueffect = new ColorizationEffect() { };
                miniumvalue = -1.0;
                maxvalue = 2.0;               
                //blueffect.Luminance = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ContrastEffect")
            {
                ContrastEffect blueffect = new ContrastEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                miniumvalue = -1.0;
                maxvalue = 1.0;
                //blueffect.Level = slider.Value;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "CurvesEffect")
            {
                CurvesEffect blueffect = new CurvesEffect() { };
                slider.Visibility = Visibility.Collapsed;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "DespeckleEffect")
            {
                DespeckleEffect blueffect = new DespeckleEffect() { };
                slider.Visibility = Visibility.Collapsed;
                //blueffect.DespeckleLevel = DespeckleLevel.High;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "ExposureEffect")
            {
                ExposureEffect blueffect = new ExposureEffect() { };
                miniumvalue = -1.0;
                maxvalue = 1.5;
                blueffect.Gain = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "GaussianNoiseEffect")
            {
                GaussianNoiseEffect blueffect = new GaussianNoiseEffect() { };
                miniumvalue = 1.0;
                maxvalue = 36;
               // blueffect.Level = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "GrayscaleEffect")
            {
                GrayscaleEffect blueffect = new GrayscaleEffect() { };
                miniumvalue = -255;
                maxvalue = 255;
                //blueffect.Constant = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "HdrEffect")
            {
                HdrEffect blueffect = new HdrEffect() { };
                miniumvalue = 0.6;
                maxvalue = 1.0;
                //blueffect.Gamma = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "HueSaturationEffect")
            {
                HueSaturationEffect blueffect = new HueSaturationEffect() { };
                miniumvalue =-1.0;
                maxvalue = 1.0;
                //blueffect.Saturation = slider.Value;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "HueSaturationLightnessEffect")
            {
                HueSaturationLightnessEffect blueffect = new HueSaturationLightnessEffect() { };
                slider.Visibility = Visibility.Collapsed;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LensBlurEffect")
            {
                LensBlurEffect blueffect = new LensBlurEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LevelsEffect")
            {
                LevelsEffect blueffect = new LevelsEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LocalBoostAutomaticEffect")
            {
                LocalBoostAutomaticEffect blueffect = new LocalBoostAutomaticEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "LocalBoostEffect")
            {
                LocalBoostEffect blueffect = new LocalBoostEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "NoiseEffect")
            {
                NoiseEffect blueffect = new NoiseEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "RgbLevelsEffect")
            {
                RgbLevelsEffect blueffect = new RgbLevelsEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "RgbMixerEffect")
            {
                RgbMixerEffect blueffect = new RgbMixerEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "SaturationLightnessEffect")
            {
                SaturationLightnessEffect blueffect = new SaturationLightnessEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "SharpnessEffect")
            {
                SharpnessEffect blueffect = new SharpnessEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "TemperatureAndTintEffect")
            {
                TemperatureAndTintEffect blueffect = new TemperatureAndTintEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "VibranceEffect")
            {
                VibranceEffect blueffect = new VibranceEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "WhiteBalanceEffect")
            {
                WhiteBalanceEffect blueffect = new WhiteBalanceEffect() { };
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                slider.Visibility = Visibility.Collapsed;
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            if (str == "WhiteboardEnhancementEffect")
            {
                WhiteboardEnhancementEffect blueffect = new WhiteboardEnhancementEffect() { };
                slider.Visibility = Visibility.Collapsed;
                m_renderer = new SwapChainPanelRenderer(blueffect, SwapChainPanelTarget);
                await ApplyEffectAsync(tranferedfile, blueffect);
            }
            slider.Minimum = miniumvalue;
            slider.Maximum = maxvalue;
        }

        private async void CopyImage_Click(object sender, RoutedEventArgs e)
        {
            using (var jpegRenderer = new JpegRenderer(currenteffect))
            {
                IBuffer jpegBuffer = await jpegRenderer.RenderAsync();
                var stream = await SavepictureHelper.Buffer2RandomAccessStream(jpegBuffer);
                DataPackage dataPackage = new DataPackage();
                dataPackage.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
                Clipboard.SetContent(dataPackage);
              //  await SavepictureHelper.Copyimage(SwapChainPanelTarget);
            }
        }
    }
}
